package com.touchapk.javafx;


import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import com.touchapk.core.AdbMonitor;

import javafx.application.Platform;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TitledPane;
import javafx.scene.control.Tooltip;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeView;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.util.Callback;

/**
 * @description
 * @author yangkeyun
 * @email coyun@qq.com
 * @date 2016年8月7日
 * @version V1.0
 */
public class TouchapkWindowController implements DialogController {

	private MainController mainController;
	
	private Console console;
	private PrintStream ps;
	
	public TouchapkWindowController(){}
	
	public TouchapkWindowController( MainController mainController){
		this.mainController=mainController;
	}
	
	private FXMLDialog dialog;

	@Override
	public void setDialog(FXMLDialog dialog) {
		this.dialog = dialog;
	}

	@FXML
	private TitledPane titledPane;

	@FXML
	private TreeView<File> treeView;

	@FXML
	private AnchorPane anchorPane;

	@FXML
	private TabPane tabPane,tabPaneconsole;
	@FXML
	private Tab tabHome,tabHomeconsole;
	
	@FXML
	private TextArea textArea;
	
	
	@FXML
	public void exitFunc(){
		this.dialog.close();
		Platform.runLater(new Runnable(){
			public void run(){
				System.exit(1);
			}
		});
	}
	
	
	@FXML
	public void selectColor(){
		FXMLDialog d = mainController.getSelectColorDialog();
		d.setResizable(false);
		d.show();
	}


	/**
	 * Initializes the controller class.
	 */
	@FXML
	// This method is called by the FXMLLoader when initialization is complete
	void initialize() {
		
		AdbMonitor.monitor();//初始化监听
	
		textArea.setWrapText(true);
		
		createMenu();
		
		console = new Console(textArea);
		ps=new PrintStream(console, true);
	 
	    redirectOutput(ps);

		this.mainController.setTreeView(treeView);
		
		titledPane.setGraphic(new ImageView(Main.class.getResource("resource_persp.gif").toExternalForm()));
		tabHome.setGraphic(new ImageView(Main.class.getResource("nav_home.gif").toExternalForm()));
		tabHomeconsole.setGraphic(new ImageView(Main.class.getResource("monitor_obj.gif").toExternalForm()));
		
		tabHome.setTooltip(new Tooltip());
		
		 Platform.runLater(new Runnable() {
             @Override
             public void run() {
            	 clreatTreeView();
             }
		 });
		
	}

	
	
	
	public void createMenu(){
		
	ContextMenu addMenu=new ContextMenu();
		

		
		MenuItem selectAllMenuItem = new MenuItem(mainController.getI18nBundle().getString("console.select.all"));
		selectAllMenuItem.setGraphic(new ImageView(Main.class.getResource("select.png").toExternalForm()));
		
		selectAllMenuItem.setOnAction(new EventHandler() {
			public void handle(Event t) {
				 Platform.runLater(new Runnable() {
		                @Override
		                public void run() {
		                	textArea.selectAll();
		                }
				 });
			}
		});
		addMenu.getItems().add(selectAllMenuItem);
		
		MenuItem copyMenuItem = new MenuItem(mainController.getI18nBundle().getString("console.copy"));
		copyMenuItem.setGraphic(new ImageView(Main.class.getResource("copy_edit.gif").toExternalForm()));
		copyMenuItem.setOnAction(new EventHandler() {
			public void handle(Event t) {
				 Platform.runLater(new Runnable() {
		                @Override
		                public void run() {
		                	textArea.copy();
		                }
				 });
			}
		});
		addMenu.getItems().add(copyMenuItem);
	
		MenuItem cleanMenuItem = new MenuItem(mainController.getI18nBundle().getString("console.clear"));
		cleanMenuItem.setGraphic(new ImageView(Main.class.getResource("clear_co.gif").toExternalForm()));
		cleanMenuItem.setOnAction(new EventHandler() {
			public void handle(Event t) {
				 Platform.runLater(new Runnable() {
		                @Override
		                public void run() {
		                	textArea.clear();
		                }
				 });
			}
		});
		addMenu.getItems().add(cleanMenuItem);
		
		textArea.setContextMenu(addMenu);
	}
	
	    private void redirectOutput(PrintStream prs) {
	        System.setOut(prs);
	        System.setErr(prs);
	    }

	    private void updateConsole(String text) {
	        for (int c : text.toCharArray()) {
	            try {
	                console.write(c);
	            } catch (IOException ex) {
	                ex.printStackTrace();
	            }
	        }
	    }

	private class PrintStream extends java.io.PrintStream {

		public PrintStream(OutputStream out, boolean autoFlush) {
			super(out, autoFlush);
		}

		@Override
		public void print(String x) {
			updateConsole(x);
		}

	}
	
	
	  
	
	private class Console extends OutputStream {

		private TextArea txtArea;

		public Console(TextArea txtArea) {
			this.txtArea = txtArea;
		}

		@Override
		public void write(int b) throws IOException {
			 Platform.runLater(new Runnable() {
	                @Override
	                public void run() {
	                	
	                	while(txtArea.getText().split("\n", -1).length > 50) {
	                        int fle = txtArea.getText().indexOf("\n");
	                        txtArea.replaceText(0, fle+1, "");
	                    }
	                	
	                	txtArea.positionCaret(txtArea.getText().length());
	                	
	                	txtArea.appendText(String.valueOf((char) b));
	                }
			 });
		}

	}

	   

	private void clreatTreeView() {

		File rfile = new File(this.mainController.getPath());

		FileTreeItem rootsitem = new FileTreeItem(rfile,true);
	
		treeView.setShowRoot(false);
		
		treeView.setRoot(rootsitem);

		
		treeView.setCellFactory(new Callback<TreeView<File>, TreeCell<File>>() {
			
			@Override
			public TreeCell<File> call(TreeView<File> arg0) {

				return new FileTreeCell(treeView, tabPane,mainController);
			}

		});

	}

}
